
//
// 130724 - AH - (Paris) Created.
// 130801 - AH - (Midtun *) toLocation().
//

package org.bisonservice;

import javax.servlet.http.HttpServletRequest;

import org.japril.jutil.CharArrayPrintWriter;

/**
 * <p>
 *   Represents the data sent from client to define a bison.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class BisonReceived
{
  private final String m_oper;
  
  private final boolean m_transformed;
  
  private final String m_uri;
  
  private final String m_mime;
  
  private final String m_turi;
  
  private final String m_tmime;
  
  public BisonReceived(HttpServletRequest req)
  {
    String oper=req.getParameter("oper");
    oper=(oper==null) ? "" : oper.trim();
    m_oper=(oper.length()==0) ? "register" : oper;
    
    String uri=req.getParameter("uri");
    uri=(uri==null) ? "" : uri.trim();
    if (uri.length()==0)
    {
      throw new IllegalArgumentException("missing uri");	
    }
    m_uri=uri;
    
    String mime=req.getParameter("mime");
    mime=(mime==null) ? "" : mime.trim();
    if (mime.length()==0)
    {
      throw new IllegalArgumentException("missing mime");	
    }
    m_mime=mime;
    
    String turi=req.getParameter("turi");
    turi=(turi==null) ? "" : turi.trim();
    
    String tmime=req.getParameter("tmime");
    tmime=(tmime==null) ? "" : tmime.trim();
    
    if (turi.length()>0 || tmime.length()>0)
    {
      m_transformed=true;
      m_turi=(turi.length()==0) ? uri : turi;
      m_tmime=(tmime.length()==0) ? mime : tmime;
    }
    else
    {
      m_transformed=false;
      m_turi=null;
      m_tmime=null;
    }
  }
  
  /**
   * <p>
   *   Gets operation.
   * </p>
   * @return Operation's name.
   */
  public String getOperation(){ return m_oper; }
  
  /**
   * <p>
   *   Tells if is transformed.
   * </p>
   * @return {@code true} if is transformed, {@code false} if is not.
   */
  public boolean isTransformed(){ return m_transformed; }
  
  /**
   * <p>
   *   Gets original MIME type.
   * </p>
   * @return Type.
   */
  public String getOriginalMime(){ return m_mime; }
  
  /**
   * <p>
   *   Gets original URI.
   * </p>
   * @return URI or {@code null} if is not known.
   */
  public String getOriginalUri(){ return m_uri; }
  
  /**
   * <p>
   *   Gets transformed URI.
   * </p>
   * @return URI or {@code null} if not known.
   */
  public String getTransformedUri(){ return m_turi; }
  
  /**
   * <p>
   *   Gets transformed MIME.
   * </p>
   * @return Transformed MIME.
   */
  public String getTransformedMime(){ return m_tmime; }
  
  /**
   * <p>
   *   Gets bison location defined by received data.
   * </p>
   * @return Location.
   */
  public String toLocation()
  {
	  StringBuilder sb=new StringBuilder();
	  String mime=(m_tmime==null) ? m_mime : m_tmime;
    String uri=(m_turi==null) ? m_uri : m_turi;
    sb.append(mime).append(':').append(uri);
    return sb.toString();
  }
  
  @Override
  public String toString()
  {
    CharArrayPrintWriter cawp=new CharArrayPrintWriter();
    cawp.println("oper="+m_oper);
    cawp.println("mime="+m_mime);
    cawp.println("uri="+m_uri);
    cawp.println("tmime="+m_tmime);
    cawp.println("turi="+m_turi);
    return cawp.toString();
  }
  
}
